package org.microservice.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.microservice.common.bean.DepartmentVo;
import org.microservice.common.util.R;
import org.microservice.common.util.TreeUtil;
import org.microservice.redis.datascope.annotation.FilterDataScope;
import org.microservice.system.dao.DepartmentDao;
import org.microservice.system.service.DepartmentLeadersService;
import org.microservice.system.service.DepartmentService;
import org.microservice.system.service.SysUserService;
import org.microservice.system.utils.dto.DepartmentTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/7/11
 * Time: 16:06
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentDao,DepartmentVo> implements DepartmentService {

    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    private DepartmentLeadersService departmentLeadersService;

    @Autowired
    private SysUserService sysUserService;


    @Override
    @FilterDataScope(ctrlTypes = {"dept"},columns = {"dd.id"})
    public List<DepartmentTree> getDepartmentListPage(Map<String, Object> params) {
        List<DepartmentVo> departmentVos = departmentDao.getDepartmentListPage(params);

        List<DepartmentTree> departmentTreeList = new ArrayList<>();
        departmentVos.forEach(departmentVo -> {
            departmentTreeList.add(new DepartmentTree(departmentVo));
        });
        return TreeUtil.bulid(departmentTreeList, 0);
    }

    @Override
    public R save(DepartmentVo departmentVo) {
        this.insert(departmentVo);
        if(departmentVo.getLeaderIds().size() > 0){
            departmentLeadersService.saveOrUpdate(departmentVo.getId(),departmentVo.getLeaderIds());
        }

        return R.ok();
    }

    @Override
    public R update(DepartmentVo departmentVo) {
        this.updateById(departmentVo);
        departmentLeadersService.saveOrUpdate(departmentVo.getId(),departmentVo.getLeaderIds());
        return R.ok();
    }

    @Override
    public List<DepartmentVo> getChildrenDepartmentByparentId(int parentId) {
        List<DepartmentVo> list = this.selectList(new EntityWrapper<DepartmentVo>()
                .eq(true,"parent_id",parentId)
                .orderBy(true,"order_num",true));
        return list;
    }

    @Override
    public DepartmentVo getDepartmentInfoById(Serializable id) {
        DepartmentVo departmentVo = this.selectById(id);
        List<Map<String, String>> leaders = sysUserService.getDepartmentLeadersByDepartmentId(id);
        departmentVo.setLeaders(leaders);
        return departmentVo;
    }

    @Override
    public void disabledOrEnabledById(int id, boolean disabled) {
        departmentDao.disabledOrEnabledById(id,disabled);
    }


}
